﻿using AppSocketModel;
using AppSocketService.Account;
using AppSocketService.Alarm;
using AppSocketService.Device;
using AppSocketService.Location;
using AppSocketService.Vehicle;
using BCGPSCommon;
using System;
using System.Linq;

namespace AppSocketService.GpsRedis
{
    public class GpsRedis
    {
        public static void Msg(string msg)
        {
            Console.WriteLine(DateTime.Now + "   " + msg);
        }

        public static void ClearRedis()
        {
            Msg("初始化Redis 开始 ");
            Msg("清空Redis");

            IRedisService redisService = new RedisService();
            int redisNo = int.Parse(ConfigurationManager.AppSettings["Redis:RedisDatabaseNo"]);
            if (IPHelper.GetHostName() == "DESKTOP-V829TES")
                redisNo = int.Parse(ConfigurationManager.AppSettings["Redis:RedisDatabaseNo-DEBUG"]);
            redisService.FlushAsync(redisNo);
        }
        #region 初始化Redis -- 企业账户
        public static void InitCompanyAccountRedis()
        { 
            IRedisService redisService = new RedisService();
 
            IVehicleService vehicleService = new VehicleService(redisService);
            IAccountService accountService = new AccountService(redisService);

            Msg("初始化Redis -- 企业账户");
            var accountList = accountService.InitRedisGetAll();
            foreach (var item in accountList)
            {
                item.Password = string.Empty;
                item.LoginIp = string.Empty; 
                if(redisService.Get<AccountDto>(GeneralContainer.RedisNameSpaceAccount + item.CompanyId.ToString()) == null)
                    redisService.AddAsync(GeneralContainer.RedisNameSpaceAccount+item.CompanyId, item);
            }
            Msg($"    Count := {accountList.Count}");

            var vehicleList = vehicleService.InitRedisGetListAll();
            foreach (var item in vehicleList)
            {
                redisService.AddHashAsync(GeneralContainer.RedisNameSpaceAccountVehicle + item.CompanyId, item.IMEI, item);  
            }

            LocationService locationService = new LocationService(redisService);
            var locationList = locationService.InitRedisGetListAll();
            foreach (var item in locationList)
            {
                redisService.AddAsync(GeneralContainer.RedisNameSpaceIMEILocation + item.IMEI, item);  //位置信息
            }
        }
        #endregion

        #region 初始化Redis -- 个人账户
        public static void InitPersonAccountRedis()
        {
            Msg("初始化个人账户");
            IRedisService redisService = new RedisService();
            AccountPersonService accountPersonService = new AccountPersonService(redisService);
            VehiclePersonService vehiclePersonService = new VehiclePersonService(redisService);
            LocationPersonService locationPersonService = new LocationPersonService(redisService);
            var accountList = accountPersonService.InitRedisGetAll();
            var vehicleList = vehiclePersonService.InitRedisGetListAll();
            var locationList = locationPersonService.InitRedisGetListAll();

            foreach (var item in accountList)
            {
                redisService.AddAsync(GeneralContainer.RedisNameSpaceAccount + item.AccountId, item);

                foreach (var vehicle in vehicleList.Where(s=>s.AccountId == item.AccountId))
                {
                    redisService.AddHashAsync(GeneralContainer.RedisNameSpaceAccountVehiclePerson + item.AccountCode, vehicle.IMEI, vehicle);
                }
                foreach (var location in locationList.Where(s=>s.AccountId == item.AccountId))
                {
                    redisService.AddAsync(GeneralContainer.RedisNameSpaceIMEILocation + location.IMEI, location); 
                }
            }
            Msg($"    Count := {accountList.Count}");
        }
        #endregion

        public static void InitAlarmCompanyRedis()
        {
            Msg("初始化企业用户报警配置");
            IRedisService redisService = new RedisService();
            var alarmList = new AlarmService().InitRedisGetAll();
            foreach (var item in alarmList)
            {
                if (item.IsAlarm)
                    redisService.AddAsync(GeneralContainer.RedisNameSpaceIMEIAlarm + item.IMEI,  item); 
            }
            Msg($"    Count := {alarmList.Count}");
        }
        public static void InitAlarmPersonRedis()
        {
            Msg("初始化个人用户报警配置");
            IRedisService redisService = new RedisService();
            var alarmList = new AlarmPersonService().InitRedisGetAll();
            foreach (var item in alarmList)
            {
                if (item.IsAlarm)
                    redisService.AddAsync(GeneralContainer.RedisNameSpaceIMEIAlarm + item.IMEI, item); 
            }
            Msg($"    Count := {alarmList.Count}");
        }

        public static void InitDeviceRedis()
        {
            Msg("初始化 设备信息");
            IRedisService redisService = new RedisService();
            IDeviceService deviceService = new DeviceService(redisService);

            var list = deviceService.InitRedisGetAll();
            foreach (var item in list)
            {
                redisService.AddAsync(GeneralContainer.RedisNameSpaceIMEIDevice + item.IMEI, item);
            }
            Msg($"    Count := {list.Count}");
        }
    }
}